﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MimosaProductsSQLite.Model;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver.Linq;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;

namespace ProductsTotalReport
{
    public class Program
    {
        static void Main(string[] args)
        {
            var mongoClient = new MongoClient("mongodb://localhost/");
            var mongoServer = mongoClient.GetServer();
            var database = mongoServer.GetDatabase("ProductReport");
            mongoServer.Connect();

            using (MimosaProductsTaxesEntities db = new MimosaProductsTaxesEntities())
            {
                using (mongoServer.RequestStart(database))
                {
                    var collection = database.GetCollection("product");
                    var products = from p in collection.AsQueryable() select p;

                    foreach (var mongoProduct in products)
                    {
                        ProductReport report = new ProductReport
                        {
                            ProductId = mongoProduct["_Id"].AsInt32,
                            ProductName = mongoProduct["Name"].AsString,
                            VendorName = mongoProduct["VendorName"].AsString,
                            TotalQuantitySold = mongoProduct["TotalQuantity"].AsInt32,
                            TotalIncomes = Convert.ToSingle(mongoProduct["TotalIncomes"].AsDouble)
                        };

                        db.ProductReports.Add(report);
                    }

                    db.SaveChanges();
                }
            }

            mongoServer.Disconnect();
        }
    }
}
